package com.hanxiaozhang.order.controller;


import com.hanxiaozhang.annotation.EnableGlobalException;
import com.hanxiaozhang.api.order.fegin.OrderFeignClient;
import com.hanxiaozhang.exception.DataNotFoundException;
import com.hanxiaozhang.order.entity.Order;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.validation.group.CreateGroupValidation;
import com.hanxiaozhang.validation.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 功能描述: <br>
 * 〈〉
 *
 * @Author:hanxinghua
 * @Date: 2020/6/28
 */
@Slf4j
@Controller
@RequestMapping("/order")
@EnableGlobalException
public class OrderConsumerController {


  @Autowired
  private OrderFeignClient orderFeignClient;

  @ResponseBody
  @GetMapping("/{id}")
  @PreAuthorize("hasAuthority('admin:order:order')")
  public Result findById(@PathVariable("id") Long id ,@RequestHeader("Authorization") String token) {
    log.info("token:[{}]",token);
    ValidationUtil.checkParameter(id!=4,"Id不能为4");
    if (id==2){
      throw  new DataNotFoundException();
    }
    return orderFeignClient.findById(id);
  }

  @ResponseBody
  @PostMapping("/save")
  @PreAuthorize("hasAuthority('admin:order:save')")
  public Result save(@RequestBody @Validated(CreateGroupValidation.class) Order order){
    return orderFeignClient.save(order);
  }



}
